package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> list = dishMapper.page(dishPageQueryDTO);
        return new PageResult(list.getTotal(),list.getResult());
    }

    @Override
    public void changeStatus(Integer status, Long id) {
        dishMapper.changeStatus(status,id);
    }

    @Override
    public DishVO findById(Long id) {
        DishVO dishVO = dishMapper.findById(id);
        List<DishFlavor> list = dishMapper.findFlavorById(id);
//        if (list != null && list.size() != 0){
//            dishVO.setFlavors(list);
//        }
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        List<Dish> dish = dishMapper.findByCategoryId(categoryId);
        return dish;
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        dishMapper.deleteByIds(ids);
    }

    @Transactional
    @Override
    public void add(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.add(dish);

        if (dishDTO.getFlavors() == null || dishDTO.getFlavors().size() == 0){
            return;
        }

        List<DishFlavor> flavors = dishDTO.getFlavors();

        flavors.forEach(e->e.setDishId(dish.getId()));

        log.info("Flavors size: " + flavors.size());
        flavors.forEach(flavor -> log.info(flavor.toString()));
        dishMapper.addFlavor(flavors);
    }

    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);


        Long dishId = dish.getId();
        if (dishDTO.getFlavors() == null || dishDTO.getFlavors().size() == 0){
            dishMapper.deleteFlavorByDishId(dishId);
            return;
        }

        List<DishFlavor> flavors = dishDTO.getFlavors();

        flavors.forEach(e->e.setDishId(dishId));

        log.info("Flavors size: " + flavors.size());
        flavors.forEach(flavor -> log.info(flavor.toString()));

        dishMapper.deleteFlavorByDishId(dishId);
        dishMapper.addFlavor(flavors);
    }
}
